﻿using Delonix_Regia_HMS.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Delonix_Regia_HMS
{
    public partial class CustomerManagement : Form
    {
        public CustomerManagement()
        {
            InitializeComponent();
        }

        //Allow only characters to be entered
        void OnlyCharacters_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
        }

        //Only only numbers to be entered
        private void onlyNumbers_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        //Update Customer
        private void btnManageCustomerUpdate_Click(object sender, EventArgs e)
        {
            string customerNRIC = tbxModifyCustomerDetailsNRIC.Text;
            string customerFirstName = tbxModifyCustomerDetailsFirstName.Text;
            string customerLastName = tbxModifyCustomerDetailsLastName.Text;
            string customerOccupation = tbxModifyCustomerDetailsOccupation.Text;
            string customerPlaceOfEmployment = tbxModifyCustomerDetailsPlaceOfEmployment.Text;
            string customerNationality = tbxModifyCustomerDetailsNationality.Text;
            string customerContactNumber = tbxModifyCustomerDetailsContactNumber.Text;
            string customerEmail = tbxModifyCustomerDetailsEmail.Text;
            string customerAddress = tbxModifyCustomerDetailsAddress.Text;
            bool update = false;
            if (string.IsNullOrEmpty(tbxModifyCustomerDetailsNRIC.Text))
            {
                MessageBox.Show("You are Missing NRIC");
            }
            else if (string.IsNullOrEmpty(tbxModifyCustomerDetailsFirstName.Text))
            {
                MessageBox.Show("You are Missing First Name");
            }
            else if (string.IsNullOrEmpty(tbxModifyCustomerDetailsLastName.Text))
            {
                MessageBox.Show("You are Missing Last Name");
            }
            else if (string.IsNullOrEmpty(tbxModifyCustomerDetailsOccupation.Text))
            {
                MessageBox.Show("You are Missing Occupation");
            }
            else if (string.IsNullOrEmpty(tbxModifyCustomerDetailsPlaceOfEmployment.Text))
            {
                MessageBox.Show("You are Missing Place Of Employment");
            }
            else if (string.IsNullOrEmpty(tbxModifyCustomerDetailsNationality.Text))
            {
                MessageBox.Show("You are Missing Nationality");
            }
            else if (string.IsNullOrEmpty(tbxModifyCustomerDetailsContactNumber.Text))
            {
                MessageBox.Show("You are Missing Contact Number");
            }
            else if (string.IsNullOrEmpty(tbxModifyCustomerDetailsEmail.Text))
            {
                MessageBox.Show("You are Missing Email");
            }
            else if (string.IsNullOrEmpty(tbxModifyCustomerDetailsAddress.Text))
            {
                MessageBox.Show("You are Missing Address");
            }
            else
            {
                DBManager.UpdateCustomer(customerNRIC, customerFirstName, customerLastName, customerOccupation, customerPlaceOfEmployment, customerNationality, customerContactNumber, customerEmail, customerAddress);                
                update = true;
            }
            if(update == true)
            {
                MessageBox.Show("Customer Updated");
            }
        }

        //Delete customer
        private void btnManageCustomerDeleteCustomer_Click(object sender, EventArgs e)
        {
            bool delete = false;
            if (string.IsNullOrEmpty(tbxModifyCustomerDetailsNRIC.Text))
            {
                MessageBox.Show("You are Missing NRIC");
            }
            else
            {
                DBManager.DeleteCustomer(tbxModifyCustomerDetailsNRIC.Text);
                delete = true;
            }
            if (delete == true)
            {
                MessageBox.Show("Customer deleted");
            }
        }

        //Create customer form
        private void btnManageCustomerCreateCustomer_Click(object sender, EventArgs e)
        {
            CreateCustomer CC = new CreateCustomer();
            CC.Show();
        }
        
        //Display all customer
        private void btnManageCustomerDisplayAllCustomer_Click(object sender, EventArgs e)
        {            
            DBManager.ListViewManageCustomerDisplayAllCustomerInformation(listViewManageCustomerCustomerInformation);
        }

        //Retrieve customer by NRIC
        private void btnManageCustomerSearchCustomerByNRIC_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbxManageCustomerCustomerNRICNumber.Text))
            {
                MessageBox.Show("Enter a customer NRIC to search");
            }
            else
            {
                DBManager.ListViewManageCustomerInformationByNRIC(listViewManageCustomerCustomerInformation, tbxManageCustomerCustomerNRICNumber.Text);
                tbxManageCustomerCustomerNRICNumber.Clear();
            }
            
        }

        //Retrieve customer by first name
        private void btnManageCustomerGetCustomerByFirstName_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbxManageCustomerCustomerFirstName.Text))
            {
                MessageBox.Show("Enter a Customer First Name to search");
            }
            else
            {
                DBManager.ListViewManageCustomerCustomerInformationByName(listViewManageCustomerCustomerInformation, tbxManageCustomerCustomerFirstName.Text);
                tbxManageCustomerCustomerFirstName.Clear();
            }
            
        }

        //Change textbox information whenver another customer is selected
        private void listViewManageCustomerCustomerInformation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewManageCustomerCustomerInformation.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in listViewManageCustomerCustomerInformation.SelectedItems)
                {
                    tbxModifyCustomerDetailsNRIC.Text = listViewManageCustomerCustomerInformation.SelectedItems[0].Text;
                    tbxModifyCustomerDetailsFirstName.Text = listViewManageCustomerCustomerInformation.SelectedItems[0].SubItems[1].Text;
                    tbxModifyCustomerDetailsLastName.Text = listViewManageCustomerCustomerInformation.SelectedItems[0].SubItems[2].Text;
                    tbxModifyCustomerDetailsOccupation.Text = listViewManageCustomerCustomerInformation.SelectedItems[0].SubItems[3].Text;
                    tbxModifyCustomerDetailsPlaceOfEmployment.Text = listViewManageCustomerCustomerInformation.SelectedItems[0].SubItems[4].Text;
                    tbxModifyCustomerDetailsNationality.Text = listViewManageCustomerCustomerInformation.SelectedItems[0].SubItems[5].Text;
                    tbxModifyCustomerDetailsContactNumber.Text = listViewManageCustomerCustomerInformation.SelectedItems[0].SubItems[6].Text;
                    tbxModifyCustomerDetailsEmail.Text = listViewManageCustomerCustomerInformation.SelectedItems[0].SubItems[7].Text;
                    tbxModifyCustomerDetailsAddress.Text = listViewManageCustomerCustomerInformation.SelectedItems[0].SubItems[8].Text;
                }
            }
        }
    }
}
